package types

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
	"strings"
)

// BaseDbModel 数据库通用字段
type BaseDbModel struct {
	Id        string         `json:"id" gorm:"type:varchar(50);primarykey"`
	CreatedAt DateTime       `json:"createdAt"`
	UpdatedAt DateTime       `json:"updatedAt"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index:deleted"`
}

// BeforeCreate 创建数据库对象之前生成UUID
func (m *BaseDbModel) BeforeCreate(*gorm.DB) (err error) {
	uuidStr := uuid.New().String()              // 生成UUID
	m.Id = strings.ReplaceAll(uuidStr, "-", "") // 去掉UUID中的"-"
	return
}
